from marketdata import statistic
from mapper import Mapper
import logging
from marketdata.models import *


class StatMover(Mapper):
    KIND = TmpMarketStats

    def map(self, key):
        tmpmarketstat = TmpMarketStats.get(key)
        marketstat = MarketStats.get_by_key_name(key.name())
        _put = True
#        logging.info("marketstat = %s",(str(marketstat)))
        _date = tmpmarketstat.updated.date()
        historykey = key.name() + _date.isoformat()
        if marketstat == None:
           _statFound = False
#           logging.info("New entry in MarketStats: typeID = %d, solarSystemID = %d" % (_typeID,_solarSystemID))
           marketstat = MarketStats(typeID = tmpmarketstat.typeID, 
                                  solarSystemID = tmpmarketstat.solarSystemID,
                                  key_name = key.name())
           marketstat.regionID = tmpmarketstat.regionID
           markethistory = None
        else: 
           _statFound = True
           if (marketstat.updated.date() > _date) :
 #               logging.info("The same data. No need update!")
               _put = False
           else:
               markethistory = MarketHistory.get_by_key_name(historykey, parent = marketstat.key())
        if _put:
            marketstat.updated = tmpmarketstat.updated
            marketstat.ordersSell = tmpmarketstat.ordersSell
            marketstat.minSellPrice = tmpmarketstat.minSellPrice
            marketstat.maxSellPrice = tmpmarketstat.maxSellPrice
            marketstat.averageSellPrice = tmpmarketstat.averageSellPrice
            marketstat.medianSellPrice = tmpmarketstat.medianSellPrice
            marketstat.volSellRemaining = tmpmarketstat.volSellRemaining
            marketstat.volSellEntered = tmpmarketstat.volSellEntered
            marketstat.newestSellOrder = tmpmarketstat.newestSellOrder
            marketstat.oldestSellOrder = tmpmarketstat.oldestSellOrder
            marketstat.ordersBuy = tmpmarketstat.ordersBuy
            marketstat.minBuyPrice = tmpmarketstat.minBuyPrice
            marketstat.maxBuyPrice = tmpmarketstat.maxBuyPrice
            marketstat.averageBuyPrice = tmpmarketstat.averageBuyPrice
            marketstat.medianBuyPrice = tmpmarketstat.medianBuyPrice
            marketstat.volBuyRemaining = tmpmarketstat.volBuyRemaining
            marketstat.volBuyEntered = tmpmarketstat.volBuyEntered
            marketstat.newestBuyOrder = tmpmarketstat.newestBuyOrder
            marketstat.oldestBuyOrder = tmpmarketstat.oldestBuyOrder
        
            if markethistory == None:
 #               logging.info("New entry in MarketHistory: date = %s, typeID = %d, solarSystemID = %d" % (_date.isoformat(),_typeID,_solarSystemID))
               markethistory = MarketHistory(typeID = marketstat.typeID, 
                                            solarSystemID = marketstat.solarSystemID,
                                            date = _date, key_name = historykey)
               markethistory.regionID = marketstat.regionID
 #            else:
 #               logging.info("Updated entry in MarketHistory: date = %s, typeID = %d, solarSystemID = %d" % (_date.isoformat(),_typeID,_solarSystemID))
            markethistory.updated = marketstat.updated
            markethistory.ordersSell = marketstat.ordersSell
            markethistory.minSellPrice = marketstat.minSellPrice
            markethistory.maxSellPrice = marketstat.maxSellPrice
            markethistory.averageSellPrice = marketstat.averageSellPrice
            markethistory.medianSellPrice = marketstat.medianSellPrice
            markethistory.volSellRemaining = marketstat.volSellRemaining
            markethistory.volSellEntered = marketstat.volSellEntered
            markethistory.newestSellOrder = marketstat.newestSellOrder
            markethistory.oldestSellOrder = marketstat.oldestSellOrder
            markethistory.ordersBuy = marketstat.ordersBuy
            markethistory.minBuyPrice = marketstat.minBuyPrice
            markethistory.maxBuyPrice = marketstat.maxBuyPrice
            markethistory.averageBuyPrice = marketstat.averageBuyPrice
            markethistory.medianBuyPrice = marketstat.medianBuyPrice
            markethistory.volBuyRemaining = marketstat.volBuyRemaining
            markethistory.volBuyEntered = marketstat.volBuyEntered
            markethistory.newestBuyOrder = marketstat.newestBuyOrder
            markethistory.oldestBuyOrder = marketstat.oldestBuyOrder

        """Updates a single entity.

        Implementers should return a tuple containing two iterables (to_update, to_delete).
        """
        return ([marketstat,markethistory], [key])

